package com.liuya.safe.client;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.liuya.entitle.CustomizedWhere;
import com.liuya.entitle.Decision;
import com.liuya.entitle.QueryResult;
import com.liuya.safe.util.Constants;
import com.liuya.safe.entitle.service.EntitleManagerService;
import com.liuya.safe.model.SafePrivilege;
import com.liuya.safe.model.SafeUser;
import com.liuya.safe.user.service.UserRoleManagerService;

public class SafeClient implements java.io.Serializable {
    private static final String appName = "safe";

    private static EntitleManagerService entitleManager;
    private static UserRoleManagerService userRoleMng;

    /**
     * Eval query policy, return query result.
     *
     * @param privilegeId
     *            privilegeId
     * @param user
     *            who queries for data
     * @param context
     *            context
     * @param where
     *            customeized where conditions
     * @return query result
     */
    public static QueryResult query(String privilegeId, SafeUser user, Map context, CustomizedWhere where) {

        if (context == null) {
            context = new HashMap();
        }
        return entitleManager.query(privilegeId, user, context, where);
    }

    /**
     * Eval query policy, return query result.
     *
     * @param privilegeId
     *            privilegeId
     * @param user
     *            who queries for data
     * @param context
     *            context
     * @return query result
     */
    public static QueryResult query(String privilegeId, SafeUser user, Map context) {
        if (context == null) {
            context = new HashMap();
        }
        return entitleManager.query(privilegeId, user, context);
    }

    /**
     * Eval query policy, return query result pagination.
     *
     * @param privilegeId
     *            privilegeId
     * @param user
     *            who queries for data
     * @param context
     *            context
     * @param where
     *            customized where condition
     * @param first
     *            first index, count from 0,1,2...
     * @param max
     *            max records
     * @return query result
     */
    public static QueryResult query(String privilegeId, SafeUser user, Map context, CustomizedWhere where, int first, int max) {
        if (context == null) {
            context = new HashMap();
        }
        return entitleManager.query(privilegeId, user, context, where, first, max);
    }

    /**
     * Eval query policy, return query result pagination.
     *
     * @param privilegeId
     *            privilegeId
     * @param user
     *            who queries for data
     * @param context
     *            context
     * @param first
     *            first index, count from 0,1,2...
     * @param max
     *            max returned records
     * @return query result
     */
    public static QueryResult query(String privilegeId, SafeUser user, Map context, int first, int max) {
        if (context == null) {
            context = new HashMap();
        }
        return entitleManager.query(privilegeId, user, context, first, max);
    }

    /**
     * Eval query policy, return result count.
     *
     * @param privilegeId
     *            privilegeId
     * @param user
     *            who queries for data
     * @param context
     *            context
     * @param where
     *            customized where condition
     * @return result count
     */
    public static int queryCount(String privilegeId, SafeUser user, Map context, CustomizedWhere where) {
        if (context == null) {
            context = new HashMap();
        }
        return entitleManager.queryCount(privilegeId, user, context, where);
    }

    /**
     * Eval query policy, return result count.
     *
     * @param privilegeId
     *            privilegeId
     * @param user
     *            who queries for data
     * @param context
     *            context
     * @return result count
     */
    public static int queryCount(String privilegeId, SafeUser user, Map context) {
        if (context == null) {
            context = new HashMap();
        }
        return entitleManager.queryCount(privilegeId, user, context);
    }

    /**
     * Eval decision policy, return decision result.
     *
     * @param privilegeId
     *            privilegeId
     * @param user
     *            who requests for this operation
     * @param businessData
     *            the business data
     * @param context
     *            context
     * @return decision result
     */
    public static Decision permit(String privilegeId, SafeUser user, Object businessData, Map context) {
        if (context == null) {
            context = new HashMap();
        }
        context.put(Constants.BUSINESS_DATA, businessData);
        return entitleManager.permit(Locale.US, privilegeId, user, context);
    }

    /**
     * Does this user be granted for the privilege?
     *
     * @param privilegeId
     *            privilege
     * @param user
     *            who
     * @return true--be granted; false--not be granted
     */
    public static boolean hasPrivilege(String privilegeId, SafeUser user) {
        Object userId = null;
        if (user != null) {
            userId = user.get(SafeUser.idFieldName);
        }
        return userRoleMng.hasPrivilege(userId, privilegeId);
    }

    /**
     * Get the user's privilege tree, often for display operate menu.
     *
     * @param user
     *            who
     * @return privilege tree(menu tree)
     */
    public static SafePrivilege getBusinessPrivilegeTree(SafeUser user) {
        Object userId = user.get(SafeUser.idFieldName);

        return userRoleMng.getBusinessPrivilegeTree(userId);
    }
}
